package com.hu.common.util.excel;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.hu.common.util.excel.config.ExcelConfig;
import com.hu.common.util.excel.dto.PersonDTO;
import com.hu.common.util.excel.dto.PositionDTO;
import com.hu.common.util.excel.handler.AbstractExcelDataHandler;
import com.hu.common.util.excel.handler.ExcelDataHandlerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 00351634
 * @version 1.0
 * @date 2024/12/21 9:13
 * @description: TODO
 */
public class ImportTest {

    /** excel header下标 */
    public static final int HEADER_INDEX = 0;
    /** excel中头的行数，默认1行 */
    public static final int HEADER_ROW_NUMBER = 1;

    public static void main(String[] args) {
        ExcelReader reader = ExcelUtil.getReader("D:\\test.xlsx");
        ExcelWriter writer = ExcelUtil.getWriter(true);
        List<Class> classList = ListUtil.of(PersonDTO.class, PositionDTO.class);
        for (Class clazz : classList) {
            AbstractExcelDataHandler excelDataHandler = ExcelDataHandlerFactory.getExcelDataHandler(clazz);
            ExcelConfig.SheetConfig sheetConfig = ExcelConfig.getSheetConfig(clazz);
            List<ExcelConfig.ColumnConfig> columnConfigList = ExcelConfig.getColumnConfig(clazz);
            reader.setSheet(sheetConfig.getIndex());
            checkHeader(reader, columnConfigList);
            Map<String, String> headerAlias = new HashMap<>();
            for (ExcelConfig.ColumnConfig columnConfig : columnConfigList) {
                headerAlias.put(columnConfig.getHeader(), columnConfig.getFieldName());
            }
            reader.setHeaderAlias(headerAlias);
            // excel中的原始数据
            List originExcelDataList = reader.read(HEADER_INDEX, HEADER_ROW_NUMBER, clazz);
            if (CollectionUtil.isNotEmpty(originExcelDataList)) {
                List validDataList = new ArrayList();
                for (Object originExcelData : originExcelDataList) {
                    if (originExcelData == null) {
                        continue;
                    }
                    Map<String, List<String>> checkResult = excelDataHandler.checkData(originExcelData);
                    if (checkResult.size() > 0) {
                        // TODO 收集异常数据和错误信息
                        System.out.println("校验失败" + checkResult);
                    } else {
                        validDataList.add(excelDataHandler.fillExtraData(originExcelData));
                    }
                }
                excelDataHandler.validDataList(validDataList);
            }
        }
    }

    class ErrorData {
        Object data;

    }

    public static void checkHeader(ExcelReader reader, List<ExcelConfig.ColumnConfig> columnConfigList) {
        List<Object> headerList = reader.readRow(HEADER_INDEX);
        Map<Integer, String> headerConfigMap = columnConfigList.stream()
            .collect(Collectors.toMap(ExcelConfig.ColumnConfig::getIndex, ExcelConfig.ColumnConfig::getHeader));
        for (int i = 0; i < headerList.size(); i++) {
            if (!ObjectUtil.equals(headerConfigMap.get(i), headerList.get(i))) {
                throw new RuntimeException(reader.getSheet().getSheetName() + "表头【" + headerList.get(i) + "】应该是【"
                    + headerConfigMap.get(i) + "】");
            }
        }
    }
}
